///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.framework.vo {
	import mx.utils.ObjectUtil;
	
	import sugarcookie.framework.Model;
	
	/**
	 * Value object that holds the data needed to navigte between Views.
	 */
	public class NavData extends Object {
		private var _view:String;
		private var _section:String;
		private var _meta:Array;
		private var _path:String;
		private var _value:String;
		private var _prevViewId:String;
		private var _initView:Boolean = false;
		
		public function NavData(v:String=null,s:String=null,m:Array=null) {
			_view = v;
			_section = s;
			_meta = m==null?[]:m;
			
			if (v!=null) _value = "/"+v;
			if (s!=null) _value += "/"+s;
			if (m!=null) _value += "/"+m.join("/");
		}
		
		public function set view(v:String):void {
			_view = v;
		}
		
		public function get view():String {
			return _view;
		}
		
		public function set section(s:String):void {
			_section = s;
		}
		
		public function get section():String {
			return _section;
		}
		
		public function set meta(m:Array):void {
			_meta = m;
		}
		
		public function get meta():Array {
			return _meta;
		}
		
		public function set path(p:String):void {
			_path = p;
		}
		
		public function get path():String {
			return _path;
		}
		
		public function set value(v:String):void {
			_value = v;
		}
		
		public function get value():String {
			return _value;
		}
		
		public function get prevViewId():String {
			return _prevViewId;
		}
		
		public function set prevViewId(id:String):void {
			_prevViewId = id;
		}
		
		public function get initView():Boolean {
			return _initView;
		}
		
		public function set initView(b:Boolean):void {
			_initView = b;
		}
		
		public static function parseNavData(swfAdrStr:String,defaultViewId:String="home"):NavData {
			var nd:NavData;
			if (swfAdrStr=="/") {
				nd = NavData.parseNavData("/"+defaultViewId);
			} else {
				var arr:Array = swfAdrStr.substr(1).split("/");
				
				//check for valid top level view
				var view:String;
				var _viewMap:Array = Model.VIEW_MAP;
				for (var i:String in _viewMap) {
					if (_viewMap[i].id==arr[0]) view = i;
				}
				if (view==null) view=defaultViewId;
				
				nd = new NavData(view);
				if (arr[1]) nd.section = arr[1];
				if (arr.length>2) {
					var mArr:Array = [];
					for (var x:int=2;x<arr.length;x++) {
						mArr.push(arr[x]);
					}
					nd.meta = mArr;
				}
			}
			nd.value = swfAdrStr;
			
			return nd;
		}
		
		public function toString():String {
			return ObjectUtil.toString(this);
		}
	}
}